package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dot.Result;
import com.qf.ssm.pojo.TbStudent;
import com.qf.ssm.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王博康
 * @version 1.0
 * @project 07_ssm
 * @description: TODO
 * @date 2022/3/10 15:20 星期四
 */
@RestController
@RequestMapping("/stu")
public class StudentController {

    private StudentService studentService;
    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @PostMapping("/add")
    public Result addStu(@RequestBody TbStudent student) {
        logger.info("添加学生的方法执行了.."+student.toString());
        studentService.addStu(student);
        return Result.setOk();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {

        logger.info("通过id={}查找学生信息...",id);
        TbStudent student = studentService.findById(id);
        logger.info("查找到的学生信息："+student.toString());
        return Result.setOk(student);
    }

    @PostMapping("/update")
    public Result updateStu(@RequestBody TbStudent student) {
        studentService.updateStu(student);
        return Result.setOk();
    }

    @RequestMapping("/find/{name}")
    public Result findByName(@PathVariable String name) {
        List<TbStudent> studentList = studentService.findByName(name);
        return Result.setOk(studentList);
    }

    @GetMapping("/find")
    public Result findAll(@RequestParam(defaultValue = "2") int pageSize, @RequestParam(defaultValue = "1") int pageNum) {
        PageInfo<TbStudent> pageInfo = studentService.findAll(pageSize, pageNum);
        return Result.setOk(pageInfo);
    }

}
